﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Senthil.Mobile.WhereAmI
{
    [Serializable]
    public class Location
    {
        string name;
        string cellTowerId;
        bool isRecognized = true;

        public Location(string name) :  this(name, "", true)
        {   
        }

        public Location(string name, string cellTowerId)
            : this(name, cellTowerId, true)
        {
        }


        public Location(string name, bool isRecognized) : this(name, "", isRecognized)
        {  
        }

        public Location(string name, string cellTowerId, bool isRecognized)
        {
            this.name = name;
            this.Id = Guid.NewGuid();
            this.isRecognized = isRecognized;
            this.cellTowerId = cellTowerId;
        }

        public Location() { }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Guid Id
        {
            get;
            set;
        }

        public string CellTowerId
        {
            get { return cellTowerId; }
            set { cellTowerId = value; }
        }

        [XmlIgnore]
        public LocationGroup LocationGroup
        {
            get;
            set;
        }

        [XmlIgnore]
        public bool IsRecognized
        {
            get { return isRecognized; }
        }

        internal void Save(StreamWriter streamWriter)
        {
            streamWriter.WriteLine(name);
        }

        internal static Location Load(StreamReader streamReader)
        {
            return new Location(streamReader.ReadLine());
        }

        public override string ToString()
        {
            return Name;
        }

        public static bool operator !=(Location l1, Location l2)
        {
            return !(l1 == l2);
        }

        public static bool operator ==(Location l1, Location l2)
        {
            if (Object.ReferenceEquals(l1, null) && Object.ReferenceEquals(l2, null))
                return true;
            else if (Object.ReferenceEquals(l1, null))
                return false;
            else
                return l1.Equals(l2);
        }

        public override bool Equals(object obj)
        {
            if (Object.ReferenceEquals(this, obj))
                return true;

            Location otherLocation = obj as Location;

            if (Object.ReferenceEquals(otherLocation, null))
                return false;

            if (!this.IsRecognized && !otherLocation.IsRecognized)
                return this.Name == otherLocation.Name && this.CellTowerId == otherLocation.CellTowerId;

            return otherLocation.Id == this.Id;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }
}
